import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;

public class BusinessOwner extends Person implements ActionListener
{

        private String business;
        private String location;
        private String manager;
        private String industry;
        JButton okE;
        JButton closeE;
        JFrame b;
        JFrame l;
        JFrame m;
        JFrame i;
        JTextField businessT;
        JTextField locationT;
        JTextField managerT;
        JTextField industryT;
    
        public BusinessOwner() throws Exception {
            business = "";
            location = "";
            manager = "";
            industry = "";
            setLayout(new GridLayout(10, 4, 5, 5));
            JLabel businessL = new JLabel("Business: ");
            businessT = new JTextField(25);
            ((AbstractDocument)businessT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
            JLabel locationL = new JLabel("Business Location: ");
            locationT = new JTextField(12);
            ((AbstractDocument)locationT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
            JLabel managerL = new JLabel("Manager: ");
            managerT = new JTextField(25);
            ((AbstractDocument)managerT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
            JLabel industryL = new JLabel("Industry: ");
            industryT = new JTextField(12);
            ((AbstractDocument)industryT.getDocument()).setDocumentFilter(new AdvisorFieldDocumentFilter());
            okE = new JButton("OK");
            closeE = new JButton("Close");
            okE.setActionCommand("OK");
            closeE.setActionCommand("CLOSE");
            okE.addActionListener(this);
            closeE.addActionListener(this);
            add(businessL);
            add(businessT);
            add(locationL);
            add(locationT);
            add(managerL);
            add(managerT);
            add(industryL);
            add(industryT);
            add(okE);
            add(closeE);
        }
    
        public boolean validateBusiness() {
            boolean valid = true;
            boolean size = true;
            char[] temp = businessT.getText().trim().toCharArray();
            for (int i = 0; i < temp.length; i++) {
                if (temp[i] < 'A' || temp[i] > 'Z') {
                    if (temp[i] < 'a' || temp[i] > 'z') {
                        if (temp[i] != ' ')
                            valid = false;
                    }
                }
            }
            if (temp.length > 25 || temp.length == 0)
                size = false;
            if (valid == false || size == false) {
                b = new JFrame("Business Error Message Box");
                b.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close = new JButton("Close");
                close.setActionCommand("BUSINESSERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL = new JLabel();
                if (valid == false && size == true) {
                    eL.setText("Only letters and spaces!");
                } else if (valid == false && size == false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                } else if (valid == true && size == false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                b.add(eL);
                b.add(close);
                b.setSize(400, 300);
                b.setLocationRelativeTo(null);
                b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                b.setVisible(true);
            }
            if (valid && size) {
                setBusiness(businessT.getText().trim());
            }
            return (valid && size);
        }
    
        public boolean validateManager() {
            boolean valid = true;
            boolean size = true;
            char[] temp = managerT.getText().trim().toCharArray();
            for (int i = 0; i < temp.length; i++) {
                if (temp[i] < 'A' || temp[i] > 'Z') {
                    if (temp[i] < 'a' || temp[i] > 'z') {
                        if (temp[i] != ' ')
                            valid = false;
                    }
                }
            }
            if (temp.length > 25 || temp.length == 0)
                size = false;
            if (valid == false || size == false) {
                m = new JFrame("Manager Error Message Box");
                m.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close = new JButton("Close");
                close.setActionCommand("MANAGERERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL = new JLabel();
                if (valid == false && size == true) {
                    eL.setText("Only letters and spaces!");
                } else if (valid == false && size == false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                } else if (valid == true && size == false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                m.add(eL);
                m.add(close);
                m.setSize(400, 300);
                m.setLocationRelativeTo(null);
                m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                m.setVisible(true);
            }
            if (valid && size) {
                setManager(managerT.getText().trim());
            }
            return (valid && size);
        }
    
        public boolean proceedOnward() {
            boolean valid = true;
            if (validateBusiness() == false)
                valid = false;
            if (validateManager() == false)
                valid = false;
            if (validateLocation() == false)
                valid = false;
            if (validateIndustry() == false)
                valid = false;
            if (validateFName() == false)
                valid = false;
            if (validateMName() == false)
                valid = false;
            if (validateLName() == false)
                valid = false;
            if (validateAge() == false)
                valid = false;
            if (validateSSN() == false)
                valid = false;
            return valid;
        }
    
        public boolean validateLocation() {
            boolean valid = true;
            boolean size = true;
            char[] temp = locationT.getText().toCharArray();
            for (int i = 0; i < temp.length; i++) {
                if (temp[i] < 'A' || temp[i] > 'Z') {
                    if (temp[i] < 'a' || temp[i] > 'z') {
                        if (temp[i] != ' ')
                            valid = false;
                    }
                }
            }
            if (temp.length> 25 || temp.length==0)
                size = false;
            if (valid == false || size == false) {
                l = new JFrame("Location Error Message Box");
                l.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close = new JButton("Close");
                close.setActionCommand("LOCATIONERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL = new JLabel();
                if (valid == false && size == true) {
                    eL.setText("Only letters and spaces!");
                } else if (valid == false && size == false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                } else if (valid == true && size == false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                l.add(eL);
                l.add(close);
                l.setSize(400, 300);
                l.setLocationRelativeTo(null);
                l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                l.setVisible(true);
            }
            if (valid && size) {
                setBusinessCompanyLocation(locationT.getText());
            }
            return (valid && size);
        }
    
        public void initialize() {
            this.setTitle("Business Owner");
            remove(ok);
            remove(close);
            this.setSize(400, 300);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        public boolean validateIndustry() {
            boolean valid = true;
            boolean size = true;
            char[] temp = locationT.getText().trim().toCharArray();
            for (int i = 0; i < temp.length; i++) {
                if (temp[i] < 'A' || temp[i] > 'Z') {
                    if (temp[i] < 'a' || temp[i] > 'z') {
                        if (temp[i] != ' ')
                            valid = false;
                    }
                }
            }
            if (temp.length > 25 || temp.length == 0)
                size = false;
            if (valid == false || size == false) {
                i = new JFrame("Industry Error Message Box");
                i.setLayout(new GridLayout(2, 1, 5, 5));
                JButton close = new JButton("Close");
                close.setActionCommand("INDUSTRYERRORCLOSEBOX");
                close.addActionListener(this);
                JLabel eL = new JLabel();
                if (valid == false && size == true) {
                    eL.setText("Only letters and spaces!");
                } else if (valid == false && size == false) {
                    eL.setText("Only letters and spaces! Fewer than twenty-five.");
                } else if (valid == true && size == false) {
                    eL.setText("A maximum of twenty-five letters and spaces.");
                }
                i.add(eL);
                i.add(close);
                i.setSize(400, 300);
                i.setLocationRelativeTo(null);
                i.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                i.setVisible(true);
            }
            if (valid && size) {
                setIndustry(industryT.getText().trim());
            }
            return (valid && size);
        }
    
        public void actionPerformed(ActionEvent event) {
                String command=event.getActionCommand();
                if(command=="OK")
                {
                        if(proceedOnward())
                        {
                            System.out.println(toString());
                            System.exit(0);
                            dispose();
                        }
                }
                else if(command=="CLOSE")
                {
                        dispose();
                        Choose c1=new Choose();
                        c1.initialize();
                }
                else if(command=="BUSINESSERRORCLOSEBOX") 
                {
                        b.dispose();
                }
                else if(command=="MANAGERERERRORCLOSEBOX") 
                {
                        m.dispose();           
                }
                else if(command=="LOCATIONERRORCLOSEBOX") 
                {
                        l.dispose();
                }
                else if(command=="INDUSTRYERRORCLOSEBOX") 
                {
                        i.dispose();
                }
        }
	public String getBusiness()
	{
		return business;
	}
	public String getBusinessCompanyLocation()
	{
		return location;
	}
	public String getManager()
	{
		return manager;
	}
	public String getIndustry()
	{
		return industry;
	}
        public void setBusiness(String temp) {
                business=temp;
        }
        public void setBusinessCompanyLocation(String temp) {
                location=temp;
        }
        public void setIndustry(String temp) {
                industry=temp;
        }
        public void setManager(String temp) {
                manager=temp;
        }
	@Override public String toString()
	{
		return "First Name: " + getFName() + "\nLast Name: " + getLName() + "\nMiddle Name: " + getMName() + "\nAge:" + getAge() + "\nSSN: " + stringSSN()+"\nBusiness: "+getBusiness()+"\nLocation: "+getBusinessCompanyLocation()+"\nManager: "+getManager()+"\nIndustry: "+getIndustry();
	}
}